import 'package:flutter/material.dart';
import 'package:yueyin/events/login_event.dart';
import 'package:yueyin/global/yin.dart';

import '../main.dart';

class LoginService extends ChangeNotifier {

  LoginService() {
    print('初始化订阅');
    bus.on<User>().listen((event) {
      print('接收到登录事件');
      login(event.username, event.password);
    });
  }

  void login(String username, String password) async {
    if (username.isEmpty || password.isEmpty) {
      print('用户名和密码不能为空');
      bus.fire(LoginEvent(false, "用户名和密码不能为空"));
      return;
    }
    var result = await Yin().dio.post('/kiss/login',
        queryParameters: {'username': username, 'password': password});

    print(result);
    if (result.data['code'] == 200) {
      bus.fire(LoginEvent(true, result.data['message']));
    } else {
      bus.fire(LoginEvent(false, result.data['message']));
    }
  }
}
